/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DaveSexton.DocProject;
using System.IO;

namespace DaveSexton.DocProject.ExternalUI
{
	internal static class Program
	{
		#region Public Properties
		public static string DocProjectInstallPath
		{
			get
			{
				return DocProjectEnvironment.InstallPath;
			}
		}

		public static string Eula
		{
			get
			{
				return Path.Combine(DocProjectInstallPath, "EULA.rtf");
			}
		}
		#endregion

		#region Private / Protected
		#endregion

		#region Methods
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		private static void Main(string[] args)
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

			ProgramForm form = new ProgramForm();
			Host host = new Host(form);

			DocProjectEnvironment.Initialize(host);

			try
			{
				SetFxCopDirEnvironmentVariable();

				if (args != null && args.Length > 0)
					form.StartupProjectFile = args[0];

				Application.Run(form);
			}
			finally
			{
				DocProjectEnvironment.Uninitialize();
			}
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")]
		private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
		{
			Log.Exception(e.Exception, Resources.Text.LogGeneralException);

			switch (MessageBox.Show(
				string.Format(System.Globalization.CultureInfo.CurrentCulture, Resources.Text.GeneralExceptionChoice, 
					Environment.NewLine, e.Exception.Message), 
				Resources.Text.GeneralExceptionCaption,
				MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1))
			{
				case DialogResult.Yes:
					Environment.FailFast(null);
					break;
			}
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
		private static void SetFxCopDirEnvironmentVariable()
		{
			try
			{
				using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
					VSEnvironmentHost.DefaultInstance.RegistryKey + @"\Setup\EDev", false))
				{
					if (key != null)
					{
						string fxCopDir = key.GetValue("FxCopDir", null) as string;

						if (!string.IsNullOrEmpty(fxCopDir))
							Environment.SetEnvironmentVariable("FxCopDir", fxCopDir, EnvironmentVariableTarget.Process);
					}
				}
			}
			catch (Exception ex)
			{
				Log.Exception(ex, Resources.Text.ErrorRetrievingFxCopyDirFromRegistry);
			}
		}
		#endregion
	}
}